//
//  WbToolBar.swift
//  Weibo
//
//  Created by HH on 16/12/5.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import SVProgressHUD

class WbToolBar: WbBaseView {
    
    var statusesViewModel:WbStatusesViewModel?{
        
        didSet{
            
            retweetBtn.setTitle(statusesViewModel?.retweetCountContent, for: .normal)
            commentBtn.setTitle(statusesViewModel?.commentCountContent, for: .normal)
            praiseBtn.setTitle(statusesViewModel?.praiseCountContent, for: .normal)
            
        }
    }
    
    //转发
    lazy private var retweetBtn:UIButton = {
       
        let btn = UIButton(title: "转发", image: #imageLiteral(resourceName: "timeline_icon_retweet"), backgroundImage: #imageLiteral(resourceName: "timeline_card_bottom_background") ,font: 13, textColor: UIColor.lightGray)
        
        btn.addTarget(self, action: #selector(retweetAction(sender:)), for: .touchUpInside)
        
        return btn
        
    }()
    
    //评论
    lazy private var commentBtn:UIButton = {
       
        let btn = UIButton(title: "评论", image: #imageLiteral(resourceName: "timeline_icon_comment"), backgroundImage: #imageLiteral(resourceName: "timeline_card_bottom_background") , font: 13, textColor: UIColor.lightGray)
        
        return btn
        
    }()
    
    //点赞
    lazy private var praiseBtn:UIButton = {
       
        let btn = UIButton(title: "赞", image: #imageLiteral(resourceName: "timeline_icon_unlike"), backgroundImage: #imageLiteral(resourceName: "timeline_card_bottom_background") , font: 13, textColor: UIColor.lightGray)
        
        return btn
        
    }()
    
//    lazy private var topLine:UIView = {
//       
//        let lineView = UIView()
//        lineView.backgroundColor = UIColor(white: 0.85, alpha: 1)
//
//        return lineView
//    }()
    
    
    
    
    override func setupUI() {
        
        backgroundColor = UIColor.white
        
        addSubview(retweetBtn)
        addSubview(commentBtn)
        addSubview(praiseBtn)
        //转发
        retweetBtn.snp.makeConstraints { (make) in
            make.top.left.bottom.equalToSuperview()
            make.width.equalTo(commentBtn)
        }
        //评论
        commentBtn.snp.makeConstraints { (make) in
            make.left.equalTo(retweetBtn.snp.right)
            make.top.bottom.equalToSuperview()
            make.width.equalTo(praiseBtn)
        }
        //赞
        praiseBtn.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(commentBtn.snp.right)
            make.right.equalToSuperview()
        }
        
        
        let firstLine = addLineView()
        let secondLine = addLineView()
//        let topLine = addLineView()
        
        
        firstLine.snp.makeConstraints { (make) in
            make.centerX.equalTo(retweetBtn.snp.right)
            make.centerY.equalToSuperview()
        }
        
        secondLine.snp.makeConstraints { (make) in
            make.centerX.equalTo(commentBtn.snp.right)
            make.centerY.equalToSuperview()
        }
        

        
    }

}

extension WbToolBar{
    
     func addLineView()->UIImageView{
        
        let lineView = UIImageView(image:#imageLiteral(resourceName: "timeline_card_bottom_line"))
        
        addSubview(lineView)
        
        return lineView
        
    }
    
}

//toolBar按钮点击事件
extension WbToolBar{
    
    @objc fileprivate func retweetAction(sender:UIButton){
        
        SVProgressHUD.show(withStatus: "转发中...")
        
        WbNetworking.sharedTools.repostStatuses(id: (statusesViewModel?.statuses?.id)!, status: nil, is_comment: 0) { (isSucess) in
            
            if isSucess{
                
                SVProgressHUD.showSuccess(withStatus: "转发成功!")
                
            }else{
                
                SVProgressHUD.showSuccess(withStatus: "转发失败!")
            }
            
        }
    }
    
}
